package com.digitalchina.user.controller;

import com.digitalchina.common.utils.Md5Utils;
import com.digitalchina.user.model.User;
import com.digitalchina.user.model.UserInfo;
import com.digitalchina.user.service.UserService;
import com.digitalchina.common.data.RtnData;
import com.digitalchina.common.pagination.Page;
import com.digitalchina.common.pagination.PaginationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    public static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 进入登陆页面
     * @return
     */
    @RequestMapping(value = "/login.html")
    public String login() {
        return "user/login";
    }
    /**
     * 进入会员管理页面
     * @return
     */
    @RequestMapping(value = "/getUserList.html")
    public String getUserList() {
        return "user/getList";
    }


    @RequestMapping(value = "/goLogin.do", method = RequestMethod.POST)
    @ResponseBody
    public RtnData goLogin(  @RequestParam(required = true) String username,
                           @RequestParam(required = true) String password,ModelMap modelMap, HttpServletRequest request) {
        Map map = new HashMap();
        map.put("username",username);
        map.put("password", Md5Utils.encodeString(password));
        int count = userService.selectBy(map);
        if(count>0){
            HttpSession session = request.getSession();
            User user = userService.getUser(map);
            session.setAttribute("userInfo",user);
            return RtnData.ok(true);
        }else{
            return RtnData.ok(false);
        }

    }
    /**
     * 注销登录
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/loginOut.do")
    @ResponseBody
    public RtnData loginOut(HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession(false);
        if (null != session) {
            session.removeAttribute("userInfo");
        }
        return RtnData.ok("注销成功");
    }
    /**
     * 查询,列表页面
     *
     * @return
     */
    @RequestMapping(value = "/getlist.do", method = RequestMethod.GET)
    public String getlist(@RequestParam(required = false, defaultValue = "10") long pageSize,
                           @RequestParam(required = false, defaultValue = "1") long page,
                           @RequestParam(required = false) String state,
                           @RequestParam(required = false) String username,
                           @RequestParam(required = false) String startTime,
                           @RequestParam(required = false) String endTime,ModelMap map, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("state", state);
        params.put("username", username);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        int totalSize = userService.selectCount(params);
        Page pagination = PaginationUtils.getPageParam(totalSize, pageSize, page); //计算出分页查询时需要使用的索引
        params.put("startIndex", pagination.getStartIndex());
        params.put("endIndex", pagination.getEndIndex());
        List users = userService.selectList(params);

        pagination.setUrl(request.getRequestURI());
        map.put("users", users);
        map.put("page", pagination);
        return "user/userTem :: userList";
    }

    /**
     * 进入编辑页面
     *
     * @param id
     * @param map
     * @return
     */
    @RequestMapping(value = "/edit.html")
    public String edit(@RequestParam Integer id, ModelMap map) {
        UserInfo userInfo = userService.detail(id);
        map.put("userInfo", userInfo);
        return "user/edit";
    }
    /**
     * 进入编辑页面
     *
     * @param userid
     * @param map
     * @return
     */
    @RequestMapping(value = "/getDetails.do")
    public String getDetails(@RequestParam Integer userid, ModelMap map){
        UserInfo userInfo = userService.detail(userid);
        map.put("userInfo", userInfo);
        return "user/detail";
    }

    /**
     * 启用用户
     *
     * @param userid
     * @param map
     * @return
     */
    @RequestMapping(value = "/onLine.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData onLine(Integer userid, ModelMap map) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserid(userid);
        userInfo.setState("0");
        int count = userService.update(userInfo);
        if(count>0){
            return RtnData.ok("启用用户成功");
        }else{
            return RtnData.fail("启用用户失败");
        }

    }
    /**
     * 更新用户信息
     *
     * @return
     */
    @RequestMapping(value = "/updateUserInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public RtnData updateUserInfo(UserInfo userInfo) {
        int count = userService.update(userInfo);
        if(count>0){
            return RtnData.ok("修改成功");
        }else{
            return RtnData.fail("修改失败");
        }
    }

    /**
     * 禁用用用户
     *
     * @param userid
     * @param map
     * @return
     */
    @RequestMapping(value = "/offLine.do", method = RequestMethod.GET)
    @ResponseBody
    public RtnData offLine(Integer userid, ModelMap map) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserid(userid);
        userInfo.setState("1");
        int count = userService.update(userInfo);
        if(count>0){
            return RtnData.ok("禁用用户成功");
        }else{
            return RtnData.fail("禁用用户失败");
        }

    }



   /*

    *//**
     * 进入新增页面
     *
     * @param map
     * @return
     *//*
    @RequestMapping(value = "/add.html")
    public String add(ModelMap map) {
        List<Category> categorys = userService.findCategorys();
        map.put("categorys", categorys);
        return "book/add";
    }

    *//**
     * 新增
     *
     * @param book
     * @param map
     * @return
     *//*
    @RequestMapping(value = "/addbook.do", method = RequestMethod.POST)
    @ResponseBody
    public RtnData add(Book book, ModelMap map) {
        userService.insertBook(book);
        return RtnData.ok("新增book成功");
    }

    *//**
     * 进入编辑页面
     *
     * @param bookid
     * @param map
     * @return
     *//*
    @RequestMapping(value = "/edit.html")
    public String edit(@RequestParam String bookid, ModelMap map) {
        Long id = Long.parseLong(bookid);
        Book book = userService.selectBookById(id);
        List<Category> categorys = userService.findCategorys();
        map.put("categorys", categorys);
        map.put("book", book);
        return "book/edit";
    }

    *//**
     * 更新
     *
     * @param book
     * @param map
     * @return
     *//*
    @RequestMapping(value = "/updatebook.do", method = RequestMethod.POST)
    @ResponseBody
    public RtnData update(Book book, ModelMap map) {
        userService.updateBook(book);
        return RtnData.ok("修改book成功");
    }

    *//**
     * 删除
     *
     * @param bookid
     * @return
     *//*
    @RequestMapping(value = "/delete.do")
    @ResponseBody
    public RtnData delete(@RequestParam String bookid) {
        Long id = Long.parseLong(bookid);
        userService.deleteBook(id);
        return RtnData.ok("删除book成功");
    }

    @RequestMapping(value = "/listBooks.do")
    @ResponseBody
    public List<Book> listBooks(@RequestParam(required = false, defaultValue = "10") long pageSize,
                                @RequestParam(required = false, defaultValue = "1") long currentPage) {
        Map<String, Object> params = new HashMap<String, Object>();
        int totalSize = userService.findTotalCount(params);
        Page page = PaginationUtils.getPageParam(totalSize, pageSize, currentPage); //计算出分页查询时需要使用的索引
        params.put("startIndex", page.getStartIndex());
        params.put("endIndex", page.getEndIndex());
        List<Book> books = userService.findAll(params);
        return books;
    }*/
}
